package org.shagin.jfin.data.util;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Currency;

/**
 * The Class CurrencyUtil.
 */
public class CurrencyUtil {

	/**
	 * Format.
	 *
	 * @param currencyCode the currency code
	 * @param value the value
	 * @return the string
	 */
	public static String format(String currencyCode, double value) {
		return format(getCurrency(currencyCode), value);
	}

	/**
	 * Format.
	 *
	 * @param currency the currency
	 * @param value the value
	 * @return the string
	 */
	public static String format(Currency currency, double value) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setCurrency(currency);
		return nf.format(value);
	}

	/**
	 * Gets the currency.
	 *
	 * @param currencyCode the currency code
	 * @return the currency
	 */
	public static Currency getCurrency(String currencyCode) {
		return Currency.getInstance(currencyCode);
	}
	
    /**
     * Parses the.
     *
     * @param source the source
     * @return the double
     * @throws ParseException the parse exception
     */
    public static double parse(String source) throws ParseException {
	    NumberFormat nf = NumberFormat.getInstance();
	    return nf.parse(source).doubleValue();
	}

}
